HKT in Rust Компилирующийся код! А в этом журнале — это редкость, полноценные монадки на расте: pub trait HKT { type C; // Current type type T; // Type with C swapped with U } macro_rules! derive_hkt { ($t:ident) => { impl HKT for $t { type C = T; type T = $t; } } } pub trait Functor: HKT { fn map(&self, f: F) -> Self::T where F: Fn(&Self::C) -> U; } pub trait Applicative: Functor { fn pure_(value: U) -> Self::T where Self: HKT; fn seq(&self, >::T) -> >::T where Self: HKT, F: Fn(&>::C) -> U; } pub trait Monad: Applicative { fn bind(&self, F) -> Self::T where F : FnMut(&Self::C) -> Self::T; fn return_(x: U) -> Self::T where Self: HKT { Self::pure_(x) } fn join(&self) -> T where Self: HKT, T: Clone { self.bind(|x| x.clone()) } } impl Functor for Vec { fn map(&self, f: F) -> Vec where F: Fn(&T) -> U { let mut result = Vec::with_capacity(self.len()); for value in self { result.push( f(value) ); } result } } impl Applicative for Vec { fn pure_(value: U) -> >::T { vec![value] } fn seq(&self, fs: >::T) -> >::T where F: Fn(&>::C) -> U { let mut result = vec![]; for (i, f) in fs.into_iter().enumerate() { let v = (f)( &self[i] ); result.push(v) } return result; } } impl Monad for Vec { fn bind(&self, mut f: F) -> Vec where F : FnMut(&T) -> Vec { let mut result = vec![]; for x in self { let v = f(x); result.extend(v); } result } } derive_hkt!(Vec); fn test() { let v = Vec::return_(1); let v = v.bind(|x| vec![x.to_string(); 3]); println!("{:?}", v); let v = vec![vec!(true), vec!(false)]; let v = v.join(); println!("{:?}", v); }